<?php
/**
 * created by zzy
 * date: 2017/11/30 16:48
 */
namespace App\Api\Service\Forms\Controllers;

use App\Eloquent\Ygt\Product;
use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Stock;
use App\Engine\Func;

class StockController extends Controller
{
    public function takein()
    {
        $userId                = Func::getHeaderValueByName( 'userid');
        $info                   = DepartmentUser::getCurrentInfo( $userId );

        $pageSize               = request( 'pageSize', 10 );
        $page                   = request( 'page', 1 );
        $offset                 = ( $page - 1 ) * $pageSize;
        $startDate             = request( 'start_date', '' );
        $endDate               = request( 'end_date', '' );

        if( !$info ){
            xThrow( 10006 );
        }
        $startTime             = Func::getFormsDateTime( $startDate );
        $endTime               = Func::getFormsDateTime( $endDate, true );
        $companyId             = $info->company_id;

        //relate_type 1：工单领取材料；2：工单预料回退；3：采购入库
        $arr                    = [
            'relate_type'=>3,'company_id'=>$companyId,
            'start_time'=>$startTime,'end_time'=>$endTime
        ];
        $data                   = Stock::getFormsData( $arr, $pageSize, $offset );
        $listCollection        = $data['lists'];
        $count                  = $data['count'];
        $maxOne                = $data['max_one'];
        $totalPriceSum        = $data['total_price_sum_all'];
        $totalPriceSumMax    = 0;
        if( $maxOne )
        {
            $totalPriceSumMax    = $maxOne->total_price_sum;
        }
        $lists                  = $productIdArr = [];
        foreach( $listCollection as $key=>$val )
        {
            $valProductId         = $val->product_id;
            $valTotalPriceSum    = $val->total_price_sum;
            $productIdArr[]       = $valProductId;
            $arr                    = [
                'product_id'=>$valProductId, 'total_price_sum'=>$valTotalPriceSum
            ];
            $lists[]                = $arr;
        }
        if( $productIdArr )
        {
            $where                  = ['id'=>['in',$productIdArr]];
            $productList           = Product::getList($where,'id,product_name');
            $productArr            = $productList->pluck('product_name', 'id');
            foreach( $lists as $key=>$val )
            {
                $valProductId          = $val['product_id'];
                $percent_round           = 0;
                if( $totalPriceSumMax > 0 )
                {
                    $percent_num          = ($val['total_price_sum']/$totalPriceSumMax) * 100;
                    $percent_round        = round( $percent_num );
                }
                $percent                  = $percent_round/100;
                $arr['percent']          = $percent;

                $title                    = '该材料已删除';
                if(isset($productArr[$valProductId]))
                {
                    $title               = $productArr[$valProductId];
                }
                $lists[$key]['title']   = $title;
                $lists[$key]['percent'] = $percent;
            }
        }
        $data               = [
            'lists'=>$lists, 'total_price_sum'=>$totalPriceSum, 'stock_num'=>$count
        ];
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $pageSize,
            'total'        => $count,
            'keyword'       => ''
        ];
        return $this->pagingData($data, $pagingData);
    }
    public function takeinList()
    {
        $userId                = Func::getHeaderValueByName( 'userid', 1 );
        $info                   = DepartmentUser::getCurrentInfo( $userId );
        if( !$info ){
            xThrow( 10006 );
        }
        $pageSize               = request( 'pageSize', 10 );
        $page                   = request( 'page', 1 );
        $offset                 = ( $page - 1 ) * $pageSize;
        $companyId             = $info->company_id;
        //relate_type 1：工单领取材料；2：工单预料回退；3：采购入库
        $where                  = ['ygt_stock.relate_type'=>3,'ygt_stock.company_id'=>$companyId];
        $fields                 = 'ygt_stock.*,a.product_name as title';
        $join                   = [
            ['table'=>'ygt_product as a', 'field_l'=>'a.id', 'field_c'=>'=', 'field_r'=>'ygt_stock.product_id'],
        ];
        $orderby                = ['ygt_stock.id','desc'];
        $stockCollection       = Stock::getList( $where,$fields,$pageSize,$offset,$orderby,'',$join );
        $stockList             = $stockCollection->toArray();
        $where                  = ['relate_type'=>3,'company_id'=>$companyId];
        $count                  = Stock::getCount( $where );
        $lists                  = [];
        foreach( $stockList as $key=>$val )
        {
            $created_atYmd     = date( 'Y-m-d', $val['created_at'] );
            $arr                = [
                'id'=>$val['id'], 'water_no'=>$val['water_no'],
                'number'=>$val['number'], 'total_price'=>$val['total_price'],
                'title'=>$val['title'], 'product_id'=>$val['product_id'],
                'created_at'=>$val['created_at'],'created_at_ymd'=>$created_atYmd,
            ];
            $lists[$key]       = $arr;
        }
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $pageSize,
            'total'        => $count,
            'keyword'       => ''
        ];
        return $this->pagingData($lists, $pagingData);
    }
}
